package com.geeker.module.sys.dict.controller;

import com.geeker.common.annotation.Log;
import com.geeker.common.api.CommonResult;
import com.geeker.common.enums.BusinessType;
import com.geeker.common.util.PageUtil;
import com.geeker.module.sys.dict.domain.SysDictData;
import com.geeker.module.sys.dict.dto.SysDictDataDto;
import com.geeker.module.sys.dict.sevice.SysDictDataService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "数据字典数据")
@RequestMapping("/sys/dict/data")
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Operation(summary = "分页")
    @PostMapping("/page")
    public CommonResult<Page<SysDictData>> page(@RequestBody SysDictDataDto dto) {
        Page<SysDictData> page = PageUtil.getPage(dto);
        Page<SysDictData> iPage = sysDictDataService.page(page);
        return CommonResult.success(iPage);
    }

    @Operation(summary = "详情")
    @PostMapping(value = "detail/{dictCode}")
    public CommonResult<SysDictData> detail(@PathVariable Long dictCode) {
        return CommonResult.success(sysDictDataService.getById(dictCode));
    }

    @Operation(summary = "根据字典类型查询字典数据信息")
    @PostMapping(value = "/type/{dictType}")
    public CommonResult<List<SysDictData>> dictType(@PathVariable String dictType) {
        return CommonResult.success(sysDictDataService.getByDictType(dictType));
    }

    @Operation(summary = "新增字典类型")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public CommonResult add(@Validated @RequestBody SysDictData data) {
        return CommonResult.success(sysDictDataService.add(data));
    }

    @Operation(summary = "编辑")
    @PostMapping("edit")
    public CommonResult edit(@Validated @RequestBody SysDictData data) {
        return CommonResult.success(sysDictDataService.edit(data));
    }

    @Operation(summary = "删除")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PostMapping("delete/{dictCodes}")
    public CommonResult delete(@PathVariable List<Long> dictCodes) {
        sysDictDataService.delete(dictCodes);
        return CommonResult.success();
    }
}
